/**
Copyright (c) 2010, Isidoro Trevino de la Garza (chololo@gmail.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
(1)Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer. 
(2)Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other 
materials provided with the distribution. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
package org.citsa.configuration

import org.citsa.ofbiz.beans.ComponentElement 

class ConfigurationManager {

	static final String CONFIG_LOCATION = "config.properties"
		
	static final String DEFAULT_CONFIG_LOCATION = "config-default.properties"
	
	static def configurationManager = new ConfigurationManager() 
		
	Properties generalProperties
	
	def slash = System.getProperty("file.separator")
	
	def loadConfiguration(){
		generalProperties = new Properties()
		generalProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(DEFAULT_CONFIG_LOCATION))
		def resource = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_LOCATION)
		if(resource){			
			def props = new Properties()
			props.load(resource)
			props.keySet().each{
				key ->
					generalProperties.setProperty(key,props.getProperty(key))
			}
		}
		checkConfiguration()		
	}
	def checkConfiguration(){
		if(!generalProperties.containsKey("ofbiz.base.installation")){
			throw new RuntimeException("Config Element 'ofbiz.base.installation' is required")
		}
	}
	
	private ConfigurationManager(){
		loadConfiguration()
	}
	
	static def instance(){
		return configurationManager
	}
	
	def getLoadFile(String dir){
		return generalProperties["ofbiz.base.installation"]+slash+dir+slash+generalProperties["ofbiz.component.load.file"]
	}
	def getComponentFile(String baseDir, String compDir){
		return generalProperties["ofbiz.base.installation"]+slash+baseDir+slash+compDir+slash+generalProperties["ofbiz.component.file"]		                                                                                                        		                                                                                                       
	}
	def getTemplateDir(){
		new File(Thread.currentThread().getContextClassLoader().getResource(generalProperties["template.dir"]).getPath())
	}
	def getTemplatePreffix(){
		return slash+generalProperties["template.dir"]
	}
	def getSiteDir(def component){
		return generalProperties["output.dir"]+slash+component.baseDir+slash+component.module
	}
	def getImagesDir(def component){
		return generalProperties["output.dir"]+slash+component.baseDir+slash+component.module+slash+"images"
	}
	def getSiteRelativeDir(def component){
		return component.baseDir+"/"+component.module
	}
		
	def getControllerPath(ComponentElement component){
		return new File(generalProperties["ofbiz.base.installation"]+slash+component.baseDir+slash+component.module+slash+component.location+slash+
		generalProperties["controller.dir"])
	}
	def getServicePath(ComponentElement component){
		return new File(generalProperties["ofbiz.base.installation"]+slash+component.baseDir+slash+component.module+slash+component.location)
	}
	
	def getComponentLoadFile(String dir){
		return new File(generalProperties["ofbiz.base.installation"]+slash+dir+slash+generalProperties["ofbiz.component.load.file"])		
	}
	
	
	def getModulePaths(){
		def appDirs = generalProperties["ofbiz.main.dirs"].split(",")
		def modules = []
		appDirs.each{
			appDir ->
			def file = new File(generalProperties["ofbiz.base.installation"]+slash+appDir)
			if(file.exists()&&file.isDirectory()){
				file.listFiles().each{
					File entry ->
					if(entry.isDirectory()){
						def src = entry.listFiles().find{ it.name=="src" }						
						if(src){
							modules << src.getAbsolutePath()
						}
					}
				}
			}
		}
		return modules
	}
}
